﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class MapGeoDecode {
    private string json;
    private GeoJson geojson;

    private double maxLon=0;
    private double minLon=1000;
    private double maxLat=0;
    private double minLat=1000;

    public GeoJson Geojson
    {
        get
        {
            return geojson;
        }
    }

    public double MaxLon
    {
        get
        {
            return maxLon;
        }

        set
        {
            maxLon = value;
        }
    }

    public double MinLon
    {
        get
        {
            return minLon;
        }

        set
        {
            minLon = value;
        }
    }

    public double MaxLat
    {
        get
        {
            return maxLat;
        }

        set
        {
            maxLat = value;
        }
    }

    public double MinLat
    {
        get
        {
            return minLat;
        }

        set
        {
            minLat = value;
        }
    }

    public MapGeoDecode(string json)
    {
        this.json = json;

        Debug.Log("解码器初始化完成");
    }
	private MapGeoDecode()
    {
        
    }
    /// <summary>
    /// 解析
    /// </summary>
    public void decode()
    {
        Debug.Log("开始解码");

        geojson = JsonConvert.DeserializeObject<GeoJson>(json);

        math();

        Debug.Log("计算完成");
        Debug.Log("maxLon:"+ maxLon);
        Debug.Log("maxLat"+ maxLat);
        Debug.Log("minLat"+ minLat);
        Debug.Log("maxLon"+ maxLon);
       
    }
    /// <summary>
    /// 计算边界值
    /// </summary>
    private void math()
    {
        foreach (Features feature in geojson.Features)
        {
            Geometry geometry = feature.Geometry;

            if (geometry.Type.Equals(Geometry.Point))
            {
                double lat = geometry.Coordinates[1].ToObject<double>();
                double lon = geometry.Coordinates[0].ToObject<double>();

                if (lat > MaxLat)
                {
                    this.MaxLat = lat;
                }
                if (lat < MinLat)
                {
                    this.MinLat = lat;
                }
                if (lon > MaxLon)
                {
                    this.MaxLon = lon;
                }
                if (lon < MinLon)
                {
                    this.MinLon = lon;
                }
            }
            else if (geometry.Type.Equals(Geometry.LineString))
            {
                foreach (JArray lineString in geometry.Coordinates)
                {
                    double lat = geometry.Coordinates[1].ToObject<double>();
                    double lon = geometry.Coordinates[0].ToObject<double>();
                    if (lat > MaxLat)
                    {
                        this.MaxLat = lat;
                    }
                    if (lat < MinLat)
                    {
                        this.MinLat = lat;
                    }
                    if (lon > MaxLon)
                    {
                        this.MaxLon = lon;
                    }
                    if (lon < MinLon)
                    {
                        this.MinLon = lon;
                    }
                }
            }
            else if (geometry.Type.Equals(Geometry.Polygon))
            {
                foreach (JArray polygon in geometry.Coordinates)
                {
                    foreach(JArray polygonchild in polygon)
                    {
                        double lat = polygonchild[1].ToObject<double>();
                        double lon = polygonchild[0].ToObject<double>();
                        if (lat > this.MaxLat)
                        {
                            this.MaxLat = lat;
                        }
                        if (lat < this.MinLat)
                        {
                            this.MinLat = lat;
                        }
                        if (lon > this.MaxLon)
                        {
                            this.MaxLon = lon;
                        }
                        if (lon < this.MinLon)
                        {
                            this.MinLon = lon;
                        }
                    }
                }
            }
            //else if (geometry.Type.Equals(Geometry.MultiPoint))
            //{

            //}
            //else if (geometry.Type.Equals(Geometry.MultiLineString))
            //{

            //}
            //else if (geometry.Type.Equals(Geometry.MultiPlygon))
            //{

            //}
            //else if (geometry.Type.Equals(Geometry.GeometryCollection))
            //{

            //}
        }
    }
    
}
